package com.cvoms.modules.app.controller;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cvoms.modules.app.entity.OrderEntity;
import com.cvoms.modules.app.service.OrderService;
import com.cvoms.modules.app.utils.IdWorker;
import com.cvoms.modules.app.vo.ConfirmOrder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cvoms.common.utils.PageUtils;
import com.cvoms.common.utils.R;



/**
 *  订单管理
 *
 * @author jinny
 * @email jinny@gmail.com
 * @date 2021-05-3 22:07:14
 */
@RestController
@RequestMapping("app/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("app:order:list")
    public R list(@RequestParam Map<String, Object> params){
        //兼容多个订单号查询
        Object orderNosObj = params.get("orderNos");
        if(orderNosObj != null) {
            String orderNoStr = (String)orderNosObj;
            String[] paramArr = orderNoStr.split("::");
            List<String> jsonArray = new ArrayList<>(paramArr.length);
            for (String id : paramArr) {
                jsonArray.add(id);
            }
            params.put("orderNoList",jsonArray);
        }

        params.put("sidx","order_time");
        params.put("order","desc");
        PageUtils page = orderService.queryPage(params);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("app:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("app:order:save")
    public R save(@RequestBody OrderEntity order){

        if (order == null) {
            return R.error(400,"参数错误");
        }


        order.setId(IdWorker.getLongCode());
        order.setOrderStatus(OrderService.ORDER_STATUS_1);
        order.setVersion(0);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());

		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("app:order:update")
    public R update(@RequestBody OrderEntity order){

        order.setUpdateTime(new Date());
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("app:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 收款确认（1/确认收款 和 0/未收到款两种状态）
     */
    @RequestMapping("/confirm")
    //@RequiresPermissions("app:order:update")
    public R confirm(@RequestBody ConfirmOrder confirmOrder){
        System.out.println("/confirm(); request parameters : "+ JSON.toJSONString(confirmOrder));

        if (confirmOrder == null || confirmOrder.getIds().length < 1) {
            return R.error("操作失败！请联系系统管理员");
        }

        long id = confirmOrder.getIds()[0];
        OrderEntity entity = orderService.getById(id);
        entity.setOrderStatus(confirmOrder.getOrderStatus());
        orderService.updateById(entity);

        return R.ok();
    }


    /**
     * 获取收款人列表
     */
    @RequestMapping("/listPayee")
    //@RequiresPermissions("app:order:update")
    public R listPayee(){
        System.out.println("/listPayee(); request parameters :");

        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<OrderEntity>();
        queryWrapper.select("DISTINCT payee");
        List<String> list = orderService.list(queryWrapper).stream().map(o -> o.getPayee()).collect(Collectors.toList());
        return R.ok().put("data", list);
    }

}
